package com.cqs.leetcode.math;

import java.util.ArrayList;
import java.util.List;

/**
 * lixw
 * 2021/11/2
 **/
public class GrayCode89 {

    public List<Integer> grayCode(int n) {
        List<Integer> list = new ArrayList<>();
        list.add(0);
        for (int i = 1; i <= n; i++) {
            List<Integer> old = list;
            list = new ArrayList<>();
            list.addAll(old);
            int delta = 1 << (i-1);
            for (int j = list.size() - 1; j >= 0; --j) {
                list.add(list.get(j )+ delta);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        GrayCode89 gray = new GrayCode89();
        for (int i = 0; i <= 10; i++) {
            List<Integer> list = gray.grayCode(i);
            System.out.println(i + " ---> "+list);
        }
    }
}
